// ProjectManage.cpp : implementation file
//

#include "stdafx.h"
#include "Measure.h"
#include "ProjectManage.h"
#include "afxdialogex.h"


// CProjectManage dialog

IMPLEMENT_DYNAMIC(CProjectManage, CDialogEx)

CProjectManage::CProjectManage(CWnd* pParent, const CMeasureSolution &solution)
	: CDialogEx(CProjectManage::IDD, pParent)
  , solution_(solution)
{
  selected_project_index_ = solution_.active_project_index()>0?0:
    solution_.active_project_index();

  project_name_       = solution_.project(selected_project_index_).name.c_str();

  boost::filesystem::path p(theApp.config().solution_path());
  p /= solution_.project(selected_project_index_).name;

  project_directory_  = p.string().c_str();
}

CProjectManage::~CProjectManage()
{
}

void CProjectManage::DoDataExchange(CDataExchange* pDX)
{
  CDialogEx::DoDataExchange(pDX);
  DDX_Text(pDX, IDC_PROJECT_NAME, project_name_);
  DDX_Text(pDX, IDC_PROJECT_DIRECTORY, project_directory_);
  DDX_LBIndex(pDX, IDC_PROJECT_LIST, selected_project_index_);
  DDX_Control(pDX, IDC_PROJECT_LIST, wnd_project_list_);
  DDX_Control(pDX, IDOK, wnd_project_delete_);
}


BEGIN_MESSAGE_MAP(CProjectManage, CDialogEx)
  ON_LBN_SELCHANGE(IDC_PROJECT_LIST, &CProjectManage::OnProjectListSelchange)
END_MESSAGE_MAP()


// CProjectManage message handlers

BOOL CProjectManage::OnInitDialog()
{
  CDialogEx::OnInitDialog();

  for(int index=0; index<solution_.NumProjects(); ++index) {
    wnd_project_list_.AddString(solution_.project(index).name.c_str());
  }

  wnd_project_list_.SetCurSel(selected_project_index_);

  if (selected_project_index_ == solution_.active_project_index()) {
    wnd_project_delete_.EnableWindow(FALSE);
  } else {
    wnd_project_delete_.EnableWindow(TRUE);
  }

  return TRUE;
}

void CProjectManage::OnProjectListSelchange()
{
  UpdateData(TRUE);

  if (selected_project_index_ == solution_.active_project_index()) {
    wnd_project_delete_.EnableWindow(FALSE);
  } else {
    wnd_project_delete_.EnableWindow(TRUE);
  }

  project_name_       = solution_.project(selected_project_index_).name.c_str();

  boost::filesystem::path p(theApp.config().solution_path());
  p /= solution_.project(selected_project_index_).name;

  project_directory_  = p.string().c_str();

  UpdateData(FALSE);
}